<?php




 
          include ('peizhi.php');
			
			
			$sql="select * from system where id=1";
			$sys=mysql_query($sql);
		    $sys=mysql_fetch_assoc($sys);
			
			$wangzhi=$sys['url'];
			
		    $wangzhi=M('system')->where('id=1')->find();
		    $wangzhi=$wangzhi['url'];
		    $url=$wangzhi."/shezhimohu.php?id=".$_POST['id']."&value=".$_POST['mohudu'];
		 
		    $result=file_get_contents($url);
			
			if($result){
			
			
			   //修改数据库orderinfo
			 
			   $sql="update orderinfo set suijistart='".$_POST['suijistart']."',suijiend='".$_POST['suijiend']."',gudingmoney='".$_POST['gudingmoney']."',type='".$_POST['type']."',mohu='".$result."'";
			   mysql_query($sql);
			   
			   
			   //获取token
			   $sql="select * from wx where id=1";
			   $wx=mysql_query($sql);
			   $wx=mysql_fetch_assoc($wx);
			   
                 $APPID=$wx['appid'];
                 $APPSECRET=$wx['appsecret'];
				 
				 
				 
				 $sql="select * from token where id=1";
				 $mytoken=mysql_query($sql);
				 $mytoken=mysql_fetch_assoc($mytoken);
				 
              


                $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;



				if(time()>$mytoken['time']){
				
				$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
				
				$json=file_get_contents($TOKEN_URL);
				$result=json_decode($json);
				
				
				$token=$result->access_token;
				
				$st['token']=$ACC_TOKEN;
				$st['time']=time()+3600;
				
				$sql="update token set token='".$ACC_TOKEN."',time='".time()."' where id=1";
				mysql_query($sql);
				
				//M('token')->where('id=1')->save($st);
				}else{
				$token=$mytoken['token'];
				}
				
				
				//打上二维码
				
					$sql="select * from orderinfo where id='".$_POST['id']."'";
		
					$order=mysql_query($sql);
					
					$order=mysql_fetch_assoc($order);
					
					$erweima="e-".$order['qingxi'].".png";
					
				  //图片上写上字
				  
				 
			  
			    include ('./xinshuiyin.php');
			   
			    $markimg = new ImageWatermark('./images/'.$result);

				$markimg->markPosType=7;
				
				$markimg->fontFile='wr.TTF';
				
				$markimg->color='#ffffff';
				
				$markimg->fontSize=22;
				
				if($order['isvideo']==1){
				
				$markimg->appendTextMark('长按二维码看视频',210,50);
				}else{
				$markimg->appendTextMark('长按二维码看图片',210,50);
				
				}
				
				$markimg->write("./images/new-".$result);
				 
				 //图片上打上二维码
				
				include ('./tupian.php');
				
				if($order['isvideo']==1){
				imageWaterMark1("./images/new-".$result, 5, "play.png");
				}
			    imageWaterMark1("./images/new-".$result, 7, "./images/".$erweima);
			   	
				
				
				
			        //上传素材
				
				$url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=image";
				$file = realpath("./images/new-".$result); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);
				
				$fromUsername=session('openid');
				//$fromUsername="o96UCwSL6bu5MzS_R0IwYTqr2yi8";
				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$token;
                $data = '{
                    "touser": "'.$fromUsername.'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			   
			   
				
			
			  $ajax=1;
			}else{
			
			  $ajax=2;
			}
			
			
		    $this->ajaxReturn($ajax);
		 
		 
	